package com.buss.customer.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buss.core.persistence.BaseDao;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.ResultList;
import com.buss.core.utils.StringUtil;
import com.buss.customer.dao.CustomerSourceDao;
import com.buss.customer.model.CustomerSource;

@Repository
public class CustomerSourceDaoImpl implements CustomerSourceDao {

	@Autowired
	private BaseDao<CustomerSource> baseDao;
	
	@Override
	public CustomerSource saveSource(CustomerSource source) {
		return baseDao.saveOrEdit(source);
	}

	@Override
	public void updateSource(CustomerSource source) {
		baseDao.edit(source);
	}

	@Override
	public void removeSource(Long[] ids) {
		String sql = "delete from CustomerSource source where source.id in (:ids)";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ids", ids);
		baseDao.excuteHsql(sql, map);
	}

	@Override
	public CustomerSource loadSource(Long id) {
		return baseDao.get(id, CustomerSource.class);
	}

	@Override
	public ResultList<CustomerSource> querySource(String companyId,
			String name, Paginalbe page) {
		String sql = "select source from CustomerSource source where source.companyId = :companyId ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		if(StringUtil.isNotEmpty(name)){
			sql += " and source.name like :name";
			params.put("name", "%" + name + "%");
		}
		return baseDao.queryHsql(sql, params, page);
	}

}
